<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 个人信息
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisBill;
use app\ais\model\AisBank;
use app\ais\model\AisBankCash;
use app\ais\model\AisCouponUser;
use app\ais\model\AisConfig;
use app\ais\model\AisVip;
use app\ais\model\AisVipUser;
use app\ais\model\AisStore;
use app\ais\model\AisVipOrder;
use app\common\facade\WechatPay;
use app\common\model\SystemUserLevel;
use think\helper\Time;

class Pay extends Base{
 
    /**
     * 微信支付开通会员
     * @param string $no
     * @return void
     */
    public function regVip(){
        if (request()->isPost()) {
            $param['vip_id'] = $this->request->param('vip_id/d');
            $this->apiSign($param);
            $vip = AisVip::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $param['vip_id']])->find();
            if(empty($vip)){
                return enjson(403,'未找到相关会员等级');
            }
            //用户VIP
            $vipUser = AisVipUser::where(['uid' => $this->user->id,'vip_id' => $vip->id])->find();
            if($vipUser){
                return enjson(403,'已开通当前会员');
            }
            //创建订单
            $order_no = order_no($this->user->invite_code);
            //唤醒微信支付参数
            $payparm = [
                'openid'     => $this->user->miniapp_uid,
                'miniapp_id' => $this->member_miniapp_id,
                'name'       => $vip->name,
                'order_no'   => $order_no,
                'total_fee'  => $vip->price * 100,
                'notify_url' => api(1,'ais/notify/vippay',$this->miniapp_id),
            ];
            //读取配置
            if($this->member_miniapp->is_psp){
                $setting = AisConfig::getConfig($this->miniapp_id);
                if (empty($setting->mchid)) {
                    return enjson(403,'服务商模式下,必须配置默认商户号');
                }
                $payparm['mchid'] = $setting->mchid;  
            }
            $ispay = WechatPay::orderPay($payparm);
            if($ispay['code'] == 0){
                return enjson(403,$ispay['msg']);
            }
            //添加订单记录
            $data['order_no']          = $order_no;
            $data['uid']               = $this->user->id;
            $data['member_miniapp_id'] = $this->member_miniapp_id;
            $data['vip_id']            = $vip->id;
            $data['amount']            = $vip->price;    //实际金额
            $data['state']             = 0;
            $data['create_time']       = time();
            $rel = AisVipOrder::create($data);
            if(!$rel){
                return enjson(403,'订单创建失败');
            }
            return enjson(200,$ispay['data']);
        }
    }
}